package org.adorsys.aderp.aderplogin.web;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.adorsys.aderp.aderplogin.aspects.BothPasswordsNotIdenticalException;
import org.adorsys.aderp.aderplogin.aspects.ExistingPasswordWrongException;
import org.adorsys.aderp.aderplogin.domain.PasswordReset;
import org.adorsys.aderp.aderplogin.service.PasswordResetService;
import org.adorsys.aderp.jpa.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

@RequestMapping("/passwordresets")
@Controller
@RooWebScaffold(path = "passwordresets", formBackingObject = PasswordReset.class)
public class PasswordResetController {
    @Autowired
    PasswordResetService passwordResetService;

//    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    // binding disabled
    public String create(@Valid PasswordReset passwordReset, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
    	throw new UnsupportedOperationException();
    }
  
//    Biding disabled
//    @RequestMapping(params = "form", produces = "text/html")
    public String createForm(Model uiModel) {
    	throw new UnsupportedOperationException();
    }
    
//  Biding disabled
//  @RequestMapping(value = "/{id}", produces = "text/html")
    public String show(@PathVariable("id") Long id, Model uiModel) {
    	throw new UnsupportedOperationException();   	
    }
    
//  Biding disabled
//  @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
    	throw new UnsupportedOperationException();   	
    }

    @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
    public String update(@Valid PasswordReset passwordReset, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
    	try {
			passwordResetService.resetPassword(passwordReset);
		} catch (BothPasswordsNotIdenticalException e) {
        	ObjectError error = new ObjectError("newPassword","Both password entered are not identical");
			bindingResult.addError(error);
		} catch (ExistingPasswordWrongException e) {
        	ObjectError error = new ObjectError("currentPassword","Current password not matching our record.");
			bindingResult.addError(error);
		}

    	if (bindingResult.hasErrors()) {
			passwordReset.setUserName(SecurityUtils.getUserName());// doesn't come back
	        uiModel.addAttribute("passwordReset", passwordReset);
	        return "passwordresets/update";
    	}
        uiModel.asMap().clear();

        return "passwordresets/confirm";
    }
    
    @RequestMapping(params = "form", produces = "text/html")
    public String updateForm(Model uiModel) {
    	PasswordReset passwordReset = new PasswordReset();
    	passwordReset.setUserName(SecurityUtils.getUserName());
        uiModel.addAttribute("passwordReset", passwordReset);
        return "passwordresets/update";
    }

    // biding disabled
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, 
    			@RequestParam(value = "size", required = false) Integer size, Model uiModel) {
    	throw new UnsupportedOperationException();   	
    }
    
    void populateEditForm(Model uiModel, PasswordReset passwordReset) {
        uiModel.addAttribute("passwordReset", passwordReset);
    }
    
    String encodeUrlPathSegment(String pathSegment, HttpServletRequest httpServletRequest) {
        String enc = httpServletRequest.getCharacterEncoding();
        if (enc == null) {
            enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
        }
        try {
            pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
        } catch (UnsupportedEncodingException uee) {}
        return pathSegment;
    }

}
